package controllers;

import java.util.ArrayList;
import java.util.List;

import models.Email;
import models.Idea;
import models.Topic;
import models.User;
import play.jobs.Every;

import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.SimpleEmail;

/**
 * The class IdeaTriggerJob extends jobs and will be executed every 6 hours, to
 * check whether the idea has comments or not..If not system will send triggers
 * (notifications and email) to the author to either delete the idea or add
 * comments to it
 * 
 * @author Nourhan El-Nehlawy
 * 
 */

@Every("6h")
public class IdeaTriggerJob extends play.jobs.Job {
	/**
	 * The method doJob() is executed every 5 hours to check if the idea is
	 * active or not If the idea is new, then it won't send triggers for the
	 * first time (notifications attribute is 30)
	 * 
	 * Then it'll keep sending triggres as long as it is inactive and
	 * notifications value is greater than 0, when notifications value is zero,
	 * system will delete the idea and notify the author
	 * 
	 * @author Nourhan El-Nehlawy
	 */
	public void doJob() throws EmailException {

		List<Idea> ideaList = Idea.findAll();

		if (!ideaList.isEmpty()) {
			for (int i = 0; i < ideaList.size(); i++) {
				Idea idea = ideaList.get(i);

				if (idea.comments.isEmpty() && idea.notifications >= 0) {
					System.out.println(ideaList.get(i).author.name);
					System.out.println(ideaList.get(i).title);

					if (idea.notifications == 30) {
						idea.notifications--;
						idea.save();
						System.out.println(idea.notifications);
					} else if (idea.notifications != 0) {
						ArrayList<User> user = new ArrayList<User>();
						user.add(idea.author);
						idea.author
								.sendNotificationWithType(
										"Please add comments to your idea or delete it",
										user, "Idea", idea.id);

						String source = "topsoft.system@gmail.com";
						String destination = idea.author.email;
						Email email = new Email(source, destination,
								"Adding comments To inactive idea",
								"Please add comments to your idea, http:localhost:9000 "
										+ idea.title + " or delete it");
						email.sendEmail();
						idea.notifications--;
						idea.save();
						System.out.println(idea.notifications);

					} else if (idea.notifications == 0) {
						System.out.println("Your idea will be deleted");

						ArrayList<User> user = new ArrayList<User>();
						user.add(idea.author);
						idea.author.sendNotificationWithType(
								"Please note that your idea will be deleted",
								user, "Idea", idea.id);

						String source = "topsoft.system@gmail.com";
						String destination = idea.author.email;
						Email email = new Email(source, destination,
								"Deleting your Idea",
								"Please note that your idea " + idea.title
										+ " will be deleted");
						email.sendEmail();

						idea.notifications--;
						idea.save();
					}
				} else if (!idea.comments.isEmpty()) {
					idea.notifications = 30;
					idea.save();
				}

			}
		}
	}
}
